<template>
    <div class="login">
        <div class="login-dl">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input type="text" placeholder="请输入账号" prefix-icon="el-icon-user-solid"
                        v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" prefix-icon="el-icon-lock"
                        v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { getmenus, getlogin } from "../utils/api"
import { fn } from "../utils/index"
export default {
    data() {
        return {
            ruleForm: {
                username: "admin",
                password: "123456"
            },
            rules: {
                username: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // this.$store.dispatch("getlogin", this.ruleForm).then(res => {
                    //     location.href = "/"
                    // })
                    getlogin(this.ruleForm).then(res => {
                        console.log(res);
                        localStorage.setItem("token", res.data.token)
                        this.login()
                        this.$router.push("/")
                        this.$message({
                            message: '登陆成功',
                            type: 'success'
                        });
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        login() {
            getmenus().then(res => {
                console.log(res);
                this.menusList = res.data
                localStorage.setItem("menus", JSON.stringify(res.data))
                let fnn = fn(res.data)
                fnn.forEach(item => {
                    this.$router.addRoute("home", item)
                })
            })
        }
    },
    created() {

    },
    mounted() {

    },
    components: {

    },
    computed: {

    },
    watch: {

    },
}
</script>

<style lang='scss' scoped>
.login {
    width: 100%;
    height: 100vh;
    background-color: #2b4b6b;
    overflow: hidden;

    .login-dl {
        width: 500px;
        height: 300px;
        background-color: #fff;
        padding: 20px;
        box-sizing: border-box;
        margin: 200px auto;

    }
}
</style>
